package cn.edu.zjgsu.ito.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.zjgsu.ito.dao.CourierOrderMapper;
import cn.edu.zjgsu.ito.dao.DemandOrderMapper;
import cn.edu.zjgsu.ito.dao.ImageUrlDemandOrderMapper;
import cn.edu.zjgsu.ito.dao.ImageUrlDemandOrderStoreMapper;
import cn.edu.zjgsu.ito.dao.LatitudeLongitudeMapper;
import cn.edu.zjgsu.ito.dao.OrderNoCourierMapper;
import cn.edu.zjgsu.ito.dao.OrderTableMapper;
import cn.edu.zjgsu.ito.dao.UserAddressMapper;
import cn.edu.zjgsu.ito.dao.UserCommentsMapper;
import cn.edu.zjgsu.ito.dao.UserInfomationMapper;
import cn.edu.zjgsu.ito.dao.UserRefundMapper;
import cn.edu.zjgsu.ito.dao.UserVouchersMapper;
import cn.edu.zjgsu.ito.dao.VouchersMapper;
import cn.edu.zjgsu.ito.model.CourierOrder;
import cn.edu.zjgsu.ito.model.CourierOrderExample;
import cn.edu.zjgsu.ito.model.DemandOrder;
import cn.edu.zjgsu.ito.model.DemandOrderExample;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrder;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderExample;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderStore;
import cn.edu.zjgsu.ito.model.ImageUrlDemandOrderStoreExample;
import cn.edu.zjgsu.ito.model.LatitudeLongitude;
import cn.edu.zjgsu.ito.model.LatitudeLongitudeExample;
import cn.edu.zjgsu.ito.model.OrderNoCourier;
import cn.edu.zjgsu.ito.model.OrderNoCourierExample;
import cn.edu.zjgsu.ito.model.OrderTable;
import cn.edu.zjgsu.ito.model.UserAddress;
import cn.edu.zjgsu.ito.model.UserComments;
import cn.edu.zjgsu.ito.model.UserInfomation;
import cn.edu.zjgsu.ito.model.UserRefund;
import cn.edu.zjgsu.ito.model.UserRefundExample;
import cn.edu.zjgsu.ito.model.UserVouchers;
import cn.edu.zjgsu.ito.model.Vouchers;
import cn.edu.zjgsu.ito.service.UserDemandServiceI;
import cn.edu.zjgsu.ito.service.UserPurchaseServiceI;
import cn.edu.zjgsu.ito.util.ConstantMethod;
import cn.edu.zjgsu.ito.util.GetTime;
import cn.edu.zjgsu.ito.util.OrderStateConstant;
import cn.edu.zjgsu.ito.vo.DemandCommentsSaveVo;
import cn.edu.zjgsu.ito.vo.DemandCommentsShowVo;
import cn.edu.zjgsu.ito.vo.DemandIdVo;
import cn.edu.zjgsu.ito.vo.DemandSaveVo;
import cn.edu.zjgsu.ito.vo.DemandSunSaveVo;
import cn.edu.zjgsu.ito.vo.OrderIdVo;
import cn.edu.zjgsu.ito.vo.OrderMessageShowVo;
import cn.edu.zjgsu.ito.vo.OrderNoCourierShowVo;
import cn.edu.zjgsu.ito.vo.OrderStateShowVo;
import cn.edu.zjgsu.ito.vo.PayPricePageShowVo;
import cn.edu.zjgsu.ito.vo.RefundReasonSaveVo;
import cn.edu.zjgsu.ito.vo.UseVouchersSaveVo;

@Service("userDemandService")
public class UserDemandServiceImpl implements UserDemandServiceI {

	@Autowired
	private DemandOrderMapper demandOrderMapper;

	@Autowired
	private OrderNoCourierMapper orderNoCourierMapper;

	@Autowired
	private CourierOrderMapper courierOrderMapper;

	@Autowired
	private UserAddressMapper userAddressMapper;

	@Autowired
	private UserInfomationMapper userInfomationMapper;

	@Autowired
	private ImageUrlDemandOrderMapper imageUrlDemandOrderMapper;
	
	@Autowired
	private ImageUrlDemandOrderStoreMapper iamgeDemandOrderStoreMapper;
	
	@Autowired
	private OrderTableMapper orderTableMapper;
	
	@Autowired
	private UserRefundMapper userRefundMapper;
	
	@Autowired
	private LatitudeLongitudeMapper latitudeLongitudeMapper;
	
	@Autowired
	private VouchersMapper vouchersMapper;
	
	@Autowired
	private UserVouchersMapper userVouchersMapper;
	
	@Autowired
	private UserCommentsMapper userCommentsMapper;

	@Autowired
	private UserPurchaseServiceI userPurchaseService; 
	
	 //地球平均半径  
    private static final double EARTH_RADIUS = 6378137;  
    //把经纬度转为度（°）  
    private static double rad(double d){  
       return d * Math.PI / 180.0;  
    }
	
	@Override
	public DemandIdVo release(DemandSaveVo demandSaveVo, int userId) {

		DemandIdVo demandIdVo = new DemandIdVo();

		DemandOrder demandOrder = new DemandOrder();

		BeanUtils.copyProperties(demandSaveVo, demandOrder);

		demandOrder.setExpectedArrivalTime(GetTime.strToDate(demandSaveVo
				.getExpectedArrivalTime()));

		demandOrder.setDemandCreatetime(GetTime.getNowTime());

		demandOrder.setDemandState(OrderStateConstant.DEMAND_NO_ACCEPT);

		demandOrder.setUserId(userId);

		demandOrderMapper.insertSelective(demandOrder);
		
		int demandId = demandOrder.getDemandId();
		System.out.println("========demandid" + demandId);
		demandIdVo.setDemandId(demandId);
		
		if(demandSaveVo.getImageUrl() != null){
			insertImage(demandId,ConstantMethod.strArrToList(demandSaveVo.getImageUrl()));
		}
		if(demandSaveVo.getStoreImageUrl() != null){
			insertImageStore(demandId, ConstantMethod.strArrToList(demandSaveVo.getStoreImageUrl()));
		}
		insertLatitudeAndLongitude(demandId,demandSaveVo);
		
		return demandIdVo;

	}

	@Override
	public DemandIdVo releaseSun(DemandSunSaveVo demandSunSaveVo, int userId) {

		DemandIdVo demandIdVo = new DemandIdVo();

		DemandOrder demandOrder = new DemandOrder();

		BeanUtils.copyProperties(demandSunSaveVo, demandOrder);

		demandOrder.setExpectedArrivalTime(GetTime.strToDate(demandSunSaveVo
				.getExpectedArrivalTime()));

		demandOrder.setDemandCreatetime(GetTime.getNowTime());

		demandOrder.setDemandState(OrderStateConstant.DEMAND_NO_ACCEPT);

		demandOrder.setUserId(userId);

		demandOrderMapper.insertSelective(demandOrder);

		int demandId = demandOrder.getDemandId();
		System.out.println("========demandid" + demandId);
		demandIdVo.setDemandId(demandId);

		if(demandSunSaveVo.getImageUrl() != null){
			insertImage(demandId,ConstantMethod.strArrToList(demandSunSaveVo.getImageUrl()));
		}
		if(demandSunSaveVo.getStoreImageUrl() != null){
			insertImageStore(demandId, ConstantMethod.strArrToList(demandSunSaveVo.getStoreImageUrl()));
		}
		
		insertSunLatitudeAndLongitude(demandId,demandSunSaveVo);

		userPurchaseService.acceptSuccess(demandIdVo, demandSunSaveVo.getPurchaseUserId());
		
		return demandIdVo;
	}
	
	
	/**
	 * 发布订单的信息详情
	 */
	@Override
	public OrderMessageShowVo releaseMessage(DemandIdVo demandIdVo) {
		OrderMessageShowVo orderMessageShowVo = new OrderMessageShowVo();

		copyOrderMessage(demandIdVo.getDemandId(), orderMessageShowVo);

		copyCourierMessage(orderMessageShowVo);

		copyImage(demandIdVo.getDemandId(), orderMessageShowVo);
		copyImageStore(demandIdVo.getDemandId(), orderMessageShowVo);
		
		return orderMessageShowVo;
	}

	@Override
	public PayPricePageShowVo payPricePage(OrderIdVo orderIdVo) {

		PayPricePageShowVo payPricePageShowVo = new PayPricePageShowVo();
		payPricePageShowVo.setOrderId(orderIdVo.getOrderId());
		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andOrderIdEqualTo(orderIdVo.getOrderId());
		List<OrderNoCourier> orderNoCourier = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		
		if(orderNoCourier != null){
			BeanUtils.copyProperties(orderNoCourier.get(0), payPricePageShowVo);
			int temp1 = Integer.parseInt(orderNoCourier.get(0).getEstimateThePrice().substring(0,orderNoCourier.get(0).getEstimateThePrice().length()-1));
			int temp2 = Integer.parseInt(orderNoCourier.get(0).getReward().substring(0,orderNoCourier.get(0).getReward().length()-1));
			int temp3 = Integer.parseInt(orderNoCourier.get(0).getCourierFees().substring(0,orderNoCourier.get(0).getCourierFees().length()-1));
			int temp4 = 0;
			if(orderNoCourier.get(0).getUserVouchersId() != null){
				
				UserVouchers userVouchers = userVouchersMapper.selectByPrimaryKey(orderNoCourier.get(0).getUserVouchersId());
				Vouchers vouchers = vouchersMapper.selectByPrimaryKey(userVouchers.getUserVouchersId());
				temp4 = Integer.valueOf(vouchers.getVouchersPrice());
				payPricePageShowVo.setVouchersName(vouchers.getVouchersName());
				payPricePageShowVo.setVouchersPrice(vouchers.getVouchersPrice());
			}
			int temp = temp1 + temp2 + temp3 - temp4;
			
			DemandOrder demandOrder = new DemandOrder();
			demandOrder.setDemandId(orderNoCourier.get(0).getDemandId());
			demandOrder.setDemandPayPrice(temp+"");
			demandOrderMapper.updateByPrimaryKeySelective(demandOrder);
			
			payPricePageShowVo.setAcount(demandOrder.getDemandPayPrice());
		}
		return payPricePageShowVo;
	}
	
	@Override
	public OrderStateShowVo payPrice(OrderIdVo orderIdVo) {
		
		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		
		/*OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderIdVo.getOrderId());
		DemandOrder demandOrder = new DemandOrder();
		demandOrder.setDemandId(orderTable.getDemandId());
		demandOrder.setDemandState(OrderStateConstant.DEMAND_PAY);
		demandOrderMapper.updateByPrimaryKeySelective(demandOrder);
		orderStateShowVo.setDemandState(OrderStateConstant.DEMAND_PAY);*/
		OrderTable orderTable = new OrderTable();
		orderTable.setOrderId(orderIdVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_PAY);
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
		
		return orderStateShowVo;
	}
	
	@Override
	public OrderIdVo payPricePageVoucher(UseVouchersSaveVo useVouchersSaveVo) {

		OrderIdVo orderIdVo = new OrderIdVo();
		
		int orderId = useVouchersSaveVo.getOrderId();
		int useVouchers = useVouchersSaveVo.getUserVouchersId();
		
		OrderTable orderTable = orderTableMapper.selectByPrimaryKey(orderId);
		int demandId = orderTable.getDemandId();
		DemandOrder demandOrder = new DemandOrder();
		demandOrder.setDemandId(demandId);
		demandOrder.setUserVouchersId(useVouchers);
		demandOrderMapper.updateByPrimaryKeySelective(demandOrder);
		orderIdVo.setOrderId(orderId);
		return orderIdVo;
	}
	
	@Override
	public OrderStateShowVo received(OrderIdVo orderIdVo) {
		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		OrderTable orderTable = new OrderTable();
		orderTable.setOrderId(orderIdVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_FINISH);
		orderTable.setOrderEndTime(GetTime.getNowTime());
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
		orderStateShowVo.setOrderState(OrderStateConstant.ORDER_FINISH);
		return orderStateShowVo;
	}
	

	@Override
	public OrderStateShowVo cancelOrder(OrderIdVo orderIdVo) {

		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		
		OrderTable orderTable = new OrderTable();
		orderTable.setOrderId(orderIdVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_CANCEL);
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
		
		orderStateShowVo.setOrderState(orderTable.getOrderState());
		
		return orderStateShowVo;
	}
	
	@Override
	public OrderStateShowVo userRefund(RefundReasonSaveVo refundReasonSaveVo) {
		OrderStateShowVo orderStateShowVo = new OrderStateShowVo();
		OrderTable orderTable = new OrderTable();
		orderTable.setOrderId(refundReasonSaveVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_REFUND);
		orderTableMapper.updateByPrimaryKeySelective(orderTable);
		
		UserRefundExample userRefundExample = new UserRefundExample();
		userRefundExample.or().andOrderIdEqualTo(refundReasonSaveVo.getOrderId());
		List<UserRefund> userRefunds = userRefundMapper.selectByExample(userRefundExample);
		
		UserRefund userRefund = new UserRefund();
		userRefund.setOrderId(refundReasonSaveVo.getOrderId());
		userRefund.setRefundReason(refundReasonSaveVo.getRefundReason());
		userRefund.setRefundState(OrderStateConstant.REFUND_NO);
		
		if(userRefunds.size() == 0){
			userRefundMapper.insert(userRefund);
		}else{
			userRefundMapper.updateByPrimaryKeySelective(userRefund);
		}
		
		orderStateShowVo.setOrderState(orderTable.getOrderState());
		return orderStateShowVo;
	}
	
	@Override
	public DemandCommentsShowVo userCommentsPage(OrderIdVo orderIdVo) {

		DemandCommentsShowVo demandCommentsShowVo = new DemandCommentsShowVo();
		
		int orderId = orderIdVo.getOrderId();
		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andOrderIdEqualTo(orderId);
		List<OrderNoCourier> orderNoCourier = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		if(orderNoCourier.size() > 0){
			int purchaseUserId = orderNoCourier.get(0).getPurchaseUserId();
			UserInfomation purchaseInfomation = userInfomationMapper.selectByPrimaryKey(purchaseUserId);
			demandCommentsShowVo.setPurchaseHeadPortraitUrl(purchaseInfomation.getHeadPortraitUrl());
			demandCommentsShowVo.setPurchaseNickname(purchaseInfomation.getNickname());
		}
			
		CourierOrderExample courierOrderExample = new CourierOrderExample();
		courierOrderExample.or().andOrderIdEqualTo(orderId);
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(courierOrderExample);
		if(courierOrders.size() > 0){
			int courierUserId = courierOrders.get(0).getCourierId();
			UserInfomation courierUserInfomation = userInfomationMapper.selectByPrimaryKey(courierUserId);
			demandCommentsShowVo.setCourierHeadPortraitUrl(courierUserInfomation.getHeadPortraitUrl());
			demandCommentsShowVo.setCourierNickname(courierUserInfomation.getNickname());
		}
		
		if(orderNoCourier.size() > 0){
			int userId = orderNoCourier.get(0).getUserId();
			UserInfomation courierUserInfomation = userInfomationMapper.selectByPrimaryKey(userId);
			demandCommentsShowVo.setDemandHeadPortraitUrl(courierUserInfomation.getHeadPortraitUrl());
			demandCommentsShowVo.setDemandNickname(courierUserInfomation.getNickname());
		}
		
		return demandCommentsShowVo;
	}
	
	@Override
	public void userDemandComments(DemandCommentsSaveVo demandCommentsSaveVo) {
		UserComments userComments = new UserComments();
		BeanUtils.copyProperties(demandCommentsSaveVo, userComments);
		
		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andOrderIdEqualTo(demandCommentsSaveVo.getOrderId());
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		if(orderNoCouriers.size() > 0){
			userComments.setPurchaseUserId(orderNoCouriers.get(0).getPurchaseUserId());
		}
		
		CourierOrderExample courierOrderExample = new CourierOrderExample();
		courierOrderExample.or().andOrderIdEqualTo(demandCommentsSaveVo.getOrderId());
		List<CourierOrder> courierOrders = courierOrderMapper.selectByExample(courierOrderExample);
		if(courierOrders.size() > 0){
			userComments.setCourierUserId(courierOrders.get(0).getCourierId());
		}
		userCommentsMapper.insertSelective(userComments);
		
		/*OrderTable orderTable = new OrderTable();
		orderTable.setOrderId(demandCommentsSaveVo.getOrderId());
		orderTable.setOrderState(OrderStateConstant.ORDER_OVER);
		orderTableMapper.updateByPrimaryKeySelective(orderTable);*/
		DemandOrder demandOrder = new DemandOrder();
		demandOrder.setDemandId(orderNoCouriers.get(0).getDemandId());
		demandOrder.setDemandState(OrderStateConstant.DEMAND_COMMENTS);
		demandOrderMapper.updateByPrimaryKeySelective(demandOrder);
	}

	@Override
	public List<OrderNoCourierShowVo> wantToBuy(int userId) {
		List<OrderNoCourierShowVo> orderNoCourierShowVos = new ArrayList<OrderNoCourierShowVo>();
		
		OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
		orderNoCourierExample.or().andUserIdEqualTo(userId);
		List<OrderNoCourier> orderNoCouriers = orderNoCourierMapper.selectByExample(orderNoCourierExample);
		
		for(int i = 0;i < orderNoCouriers.size();i++){
			OrderNoCourierShowVo orderNoCourierShowVo = new OrderNoCourierShowVo();
			BeanUtils.copyProperties(orderNoCouriers.get(i),orderNoCourierShowVo);
			orderNoCourierShowVos.add(orderNoCourierShowVo);
		}
		return orderNoCourierShowVos;
	}
	
	/**
	 * 复制订单信息
	 */
	public OrderMessageShowVo copyOrderMessage(int demandId,
			OrderMessageShowVo orderMessageShowVo) {

		DemandOrderExample demandOrderExample = new DemandOrderExample();
		demandOrderExample.or().andDemandIdEqualTo(demandId);
		List<DemandOrder> demandOrders = demandOrderMapper
				.selectByExample(demandOrderExample);

		if (demandOrders.get(0).getDemandState() != 0) {// 复制订单视图信息
			OrderNoCourierExample orderNoCourierExample = new OrderNoCourierExample();
			orderNoCourierExample.or().andDemandIdEqualTo(demandId);
			List<OrderNoCourier> orderNoCourier = orderNoCourierMapper
					.selectByExample(orderNoCourierExample);

			BeanUtils.copyProperties(orderNoCourier.get(0), orderMessageShowVo);
			// 复制时间信息
			orderMessageShowVo.setExpectedArrivalTime(GetTime
					.dateToStr(orderNoCourier.get(0).getExpectedArrivalTime()));
			orderMessageShowVo.setDemandCreatetime(GetTime
					.dateToStr(orderNoCourier.get(0).getDemandCreatetime()));
			orderMessageShowVo.setAcceptTime(GetTime.dateToStr(orderNoCourier
					.get(0).getAcceptTime()));
			// 添加代购人姓名信息
			UserInfomation userInfomation = userInfomationMapper
					.selectByPrimaryKey(orderNoCourier.get(0)
							.getPurchaseUserId());
			orderMessageShowVo.setPurchaseName(userInfomation.getNickname());
			if(orderNoCourier.get(0).getOrderEndTime() != null){
				orderMessageShowVo.setOrderEndTime(GetTime.dateToStr(orderNoCourier.get(0).getOrderEndTime()));
			}
			
			//添加求购者代金券信息
			if(orderNoCourier.get(0).getUserVouchersId() != null){
				orderMessageShowVo.setVouchersPrice(copyVouchersPrcice(orderNoCourier.get(0).getUserVouchersId()));
			}
			
			
		} else {
			BeanUtils.copyProperties(demandOrders.get(0), orderMessageShowVo);
			
			LatitudeLongitudeExample latitudeLongitudeExample = new LatitudeLongitudeExample();
			latitudeLongitudeExample.or().andDemandIdEqualTo(demandId);
			List<LatitudeLongitude> latitudeLongitude = latitudeLongitudeMapper.selectByExample(latitudeLongitudeExample);
			
			BeanUtils.copyProperties(latitudeLongitude.get(0), orderMessageShowVo);
			
			orderMessageShowVo.setExpectedArrivalTime(GetTime
					.dateToStr(demandOrders.get(0).getExpectedArrivalTime()));
			orderMessageShowVo.setDemandCreatetime(GetTime
					.dateToStr(demandOrders.get(0).getDemandCreatetime()));
			orderMessageShowVo.setOrderId(-1);
		}

		UserAddress userAddress = userAddressMapper
				.selectByPrimaryKey(demandOrders.get(0).getAddressId());
		System.out.println("demandOrders.get(0).getAddressId()" + demandOrders.get(0).getAddressId());
		System.out.println("userAddress" + userAddress.getAddressDetails()+ userAddress.getAddressLongitude());
		
		BeanUtils.copyProperties(userAddress, orderMessageShowVo);

		return orderMessageShowVo;
	}

	/**
	 * 复制快递员信息
	 * 
	 * @param orderMessageShowVo
	 * @param demandId
	 * @return
	 */
	public OrderMessageShowVo copyCourierMessage(
			OrderMessageShowVo orderMessageShowVo) {

		int orderId = orderMessageShowVo.getOrderId();

		if (orderId == -1) {
			return orderMessageShowVo;
		}

		CourierOrderExample example2 = new CourierOrderExample();
		example2.or().andOrderIdEqualTo(orderId);
		List<CourierOrder> courierOrder = courierOrderMapper
				.selectByExample(example2);

		if (courierOrder.size() > 0) {
			BeanUtils.copyProperties(courierOrder.get(0), orderMessageShowVo);
			orderMessageShowVo.setCourierAcceptTime(GetTime.dateToStr(courierOrder.get(0).getCourierAcceptTime()));
			
			UserInfomation userInfomation = userInfomationMapper.selectByPrimaryKey(courierOrder.get(0).getCourierId());
			
			orderMessageShowVo.setCourierName(userInfomation.getNickname());
			if(courierOrder.get(0).getArriveTime() != null){
				orderMessageShowVo.setArriveTime(GetTime.dateToStr(courierOrder.get(0).getArriveTime()));
			}
		}
		return orderMessageShowVo;
	}

	/**
	 * 存商品图片
	 * @param demandId
	 * @param goods
	 * @return
	 */
	public int insertImage(int demandId,List<String> goods){
		System.out.println("asdasdasd" + demandId);
		if(goods != null){
			System.out.println("图片size" + goods.size());
			for(int i = 0;i < goods.size();i++){
				System.out.println("转完之后的" + goods.get(i));
				ImageUrlDemandOrderExample example = new ImageUrlDemandOrderExample();
				example.or().andImageUrlEqualTo(goods.get(i));
				List<ImageUrlDemandOrder> imageUrlDemandOrder = imageUrlDemandOrderMapper.selectByExample(example);
				for(int j = 0;j < imageUrlDemandOrder.size();j++){
					int id = imageUrlDemandOrder.get(j).getId();
					System.out.println("size======"+imageUrlDemandOrder.size());
					ImageUrlDemandOrder imageUrlDemandOrder2 = new ImageUrlDemandOrder();
					imageUrlDemandOrder2.setId(id);
					imageUrlDemandOrder2.setDemandId(demandId);
					System.out.println("这个是id"+id);
					imageUrlDemandOrderMapper.updateByPrimaryKeySelective(imageUrlDemandOrder2);
					System.out.println("jiehsu ========");
				}
//				ImageUrlDemandOrder imageUrlDemandOrder = new ImageUrlDemandOrder();
//				imageUrlDemandOrder.setDemandId(demandId);
//				System.out.println("====chuanfguolai" + demandId);
//				imageUrlDemandOrderMapper.updateByExampleSelective(imageUrlDemandOrder, example);
			}
		}
		return 1;
	}
	
	/**
	 * 存店铺图片
	 * @param demandId
	 * @param goods
	 * @return
	 */
	public int insertImageStore(int demandId,List<String> goodsStore){
		if(goodsStore != null){
			for(int i = 0;i < goodsStore.size();i++){
				ImageUrlDemandOrderStoreExample example = new ImageUrlDemandOrderStoreExample();
				example.or().andStoreImageUrlEqualTo(goodsStore.get(i));
				ImageUrlDemandOrderStore imageUrlDemandOrderStore = new ImageUrlDemandOrderStore();
				imageUrlDemandOrderStore.setDemandId(demandId);
				iamgeDemandOrderStoreMapper.updateByExampleSelective(imageUrlDemandOrderStore, example);
			}
		}
		return 1;
	}
	
	/**
	 * 复制商品图片
	 * @param demandId
	 * @param orderMessageShowVo
	 * @return
	 */
	public OrderMessageShowVo copyImage(int demandId,OrderMessageShowVo orderMessageShowVo){
		
		List<String> iamgeList = new ArrayList<String>();
		
		ImageUrlDemandOrderExample imageUrlDemandOrderExample = new ImageUrlDemandOrderExample();
		imageUrlDemandOrderExample.or().andDemandIdEqualTo(demandId);
		List<ImageUrlDemandOrder> images = imageUrlDemandOrderMapper.selectByExample(imageUrlDemandOrderExample);
		for(int i = 0;i < images.size();i++){
			iamgeList.add(images.get(i).getImageUrl());
		}
		
		orderMessageShowVo.setTradeUrlArrayList((ArrayList<String>) iamgeList);
		
		return orderMessageShowVo;
	}
	
	
	/**
	 * 复制商店图片
	 * @param demandId
	 * @param orderMessageShowVo
	 * @return
	 */
	public OrderMessageShowVo copyImageStore(int demandId,OrderMessageShowVo orderMessageShowVo){
		
		List<String> iamgeList = new ArrayList<String>();
		
		ImageUrlDemandOrderStoreExample imageUrlDemandOrderStoreExample = new ImageUrlDemandOrderStoreExample();
		imageUrlDemandOrderStoreExample.or().andDemandIdEqualTo(demandId);
		List<ImageUrlDemandOrderStore> images = iamgeDemandOrderStoreMapper.selectByExample(imageUrlDemandOrderStoreExample);
		for(int i = 0;i < images.size();i++){
			iamgeList.add(images.get(i).getStoreImageUrl());
		}
		
		orderMessageShowVo.setStoreUrlArrayList((ArrayList<String>) iamgeList);
		
		return orderMessageShowVo;
	}
	
	//插入经纬度
	public int insertLatitudeAndLongitude(int demandId,DemandSaveVo demandSaveVo){
		
		UserAddress userAddress = userAddressMapper.selectByPrimaryKey(demandSaveVo.getAddressId());
		
		LatitudeLongitude latitudeLongitude = new LatitudeLongitude();
		
		BeanUtils.copyProperties(userAddress, latitudeLongitude);
		BeanUtils.copyProperties(demandSaveVo, latitudeLongitude);
		latitudeLongitude.setDemandId(demandId);
		
		latitudeLongitudeMapper.insertSelective(latitudeLongitude);
		return 1;
	}
	
	// 阳光模式插入经纬度
		public int insertSunLatitudeAndLongitude(int demandId,DemandSunSaveVo demandSunSaveVo){
			
			UserAddress userAddress = userAddressMapper.selectByPrimaryKey(demandSunSaveVo.getAddressId());
			
			LatitudeLongitude latitudeLongitude = new LatitudeLongitude();
			
			BeanUtils.copyProperties(userAddress, latitudeLongitude);
			BeanUtils.copyProperties(demandSunSaveVo, latitudeLongitude);
			latitudeLongitude.setDemandId(demandId);
			
			latitudeLongitudeMapper.insertSelective(latitudeLongitude);
			return 1;
		}
	
	//查询代金券抵的钱数
	public String copyVouchersPrcice(int userVouchersId){
		UserVouchers userVouchers = userVouchersMapper.selectByPrimaryKey(userVouchersId);
		Vouchers vouchers = vouchersMapper.selectByPrimaryKey(userVouchers.getVouchersId());
		return vouchers.getVouchersPrice();
		
	}
	
	//计算快递费
	public static String getDistance(String lng1, String lat1, String lng2, String lat2){  
		double radLat1 = rad(Double.valueOf(lat1));
		double radLat2 = rad(Double.valueOf(lat2));
		double a = radLat1 - radLat2;  
		double b = rad(Double.valueOf(lng1)) - rad(Double.valueOf(lng2));
		double s = 2 * Math.asin(  
				Math.sqrt(  
						Math.pow(Math.sin(a/2),2)   
						+ Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)  
						)  
				);  
		s = s * EARTH_RADIUS;  
		s = Math.round(s * 10000) / 10000;
		int endDis = (int)s / 1000   + 1;
		String money = "";
		if(endDis <= 5){
			System.out.println("价格"+10);
			money = "10元";
		}else{
			money = (10 + (endDis-5)) + "元";
		}
		return money;  
	} 

}
